package com.mmall.concurrency.AQS;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class FutureDemo {
    // 首先声明一个callbale
    static class MyCallable implements Callable<String>{

        @Override
        public String call() throws Exception {
            log.info("do something in callable!");
            Thread.sleep(5000);
            return "Done OK!";
        }
    }
    // 在main方法测试
    public static void main(String[] args) throws Exception{
        // 在线程池里执行
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> future = executorService.submit( new MyCallable() );
        log.info("do something in main!");
        Thread.sleep(2000);

        // 没有获取到，就在这里阻塞
        String result = future.get();
        log.info("{}", result);
    }

}
